//
//  TodayListSubViewController.m
//  UI_A段项目_iDaily Watch
//
//  Created by 刘旭 on 14-10-16.
//  Copyright (c) 2014年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "TodayListSubViewController.h"
#import "TodayListSubCellOneTableViewCell.h"
#import "TodayListSubCellTwoTableViewCell.h"
#import "TodayListSubCellOne.h"
#import "TodayListSubCellTwo.h"
#import "TodayListSubCellThree.h"
#import "AFHTTPRequestOperationManager.h"
#import "UIButton+WebCache.h"
//list页面链接页
@interface TodayListSubViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, retain)NSMutableArray *dataArrOne;
@property(nonatomic, retain)NSMutableArray *dataArrTwo;
@property(nonatomic, retain)NSMutableArray *dataArrThree;
@property(nonatomic, retain)UITableView *tableView;
@property(nonatomic, retain)UIWebView *webView;
@end

@implementation TodayListSubViewController
- (void)dealloc
{
    [_dataArrOne release];
    [_dataArrTwo release];
    [_dataArrThree release];
    [super dealloc];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        [self getData];
        
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self tableView1];
    // Do any additional setup after loading the view.
}
- (void)getData
{
    self.dataArrOne = [NSMutableArray array];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:@"http://watch-cdn.idailywatch.com/api/list/cover/zh-hans?page=1&ver=iphone&app_ver=9" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSArray * arr = [[responseObject objectAtIndex:_count - 1000] objectForKey:@"album"]; //判断进第几个字典.
        for (NSDictionary *dicc in arr) {
            TodayListSubCellOne *sub = [[TodayListSubCellOne alloc]init];
            sub.cover_thumb1 = [dicc objectForKey:@"cover_thumb"];
            [_dataArrOne addObject:sub];
            [_tableView reloadData];

        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    }];
    
    self.dataArrTwo = [NSMutableArray array];
    AFHTTPRequestOperationManager *manager1 = [AFHTTPRequestOperationManager manager];
    [manager1 GET:@"http://watch-cdn.idailywatch.com/api/list/cover/zh-hans?page=1&ver=iphone&app_ver=9" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        for (NSMutableArray *arr2 in responseObject) {
            TodayListSubCellTwo *sub1 = [[TodayListSubCellTwo alloc]init];
            arr2 = [NSMutableArray array];
            arr2 = responseObject;
            
            sub1.title = [arr2[_count - 1000] objectForKey:@"title"];
            sub1.source = [arr2[_count - 1000] objectForKey:@"source"];
            sub1.pubdate = [arr2[_count - 1000] objectForKey:@"pubdate"];
            sub1.author = [arr2[_count - 1000] objectForKey:@"author"];
            [_dataArrTwo addObject:sub1];
            [_tableView reloadData];

        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    }];
    
    self.dataArrThree = [NSMutableArray array];
    AFHTTPRequestOperationManager *manager2 = [AFHTTPRequestOperationManager manager];
    [manager2 GET:@"http://watch-cdn.idailywatch.com/api/list/cover/zh-hans?page=1&ver=iphone&app_ver=9" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        for (NSMutableArray *arr2 in responseObject) {
            TodayListSubCellThree *sub2 = [[TodayListSubCellThree alloc]init];
            arr2 = [NSMutableArray array];
            arr2 = responseObject;
            
            sub2.content = [arr2[_count - 1000] objectForKey:@"content"];
            [_dataArrThree addObject:sub2];
            NSLog(@"%@", sub2.content);
            [_tableView reloadData];
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];

   
}
- (void)tableView1
{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, 480 )];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return 110;
    }
    else if(indexPath.row == 1)
    {
        return 85;
    }
    else
        return 1000;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (_dataArrOne.count == 0 || _dataArrTwo.count == 0 || _dataArrThree.count == 0) {
        return 0;
    }
    return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        static NSString *identifier = @"cellOne";
        TodayListSubCellOneTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (!cell) {
            cell = [[TodayListSubCellOneTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
        }
//         NSLog(@"%lu", _dataArrOne.count);
        cell.selectionStyle = NO;
        for (int i = 0; i < 5; i++) {
            cell.button = (UIButton *)[cell viewWithTag:i + 1];
            [cell.button setBackgroundImageWithURL:[NSURL URLWithString:[_dataArrOne[(indexPath.row) + i]cover_thumb1]] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"t11.png"]];
        }
        return cell;
    }
    else if(indexPath.row == 1){
        static NSString *identifier = @"cellTwo";
        TodayListSubCellTwoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (!cell) {
            cell = [[TodayListSubCellTwoTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
        }
        cell.selectionStyle = NO;
        cell.label1.text = [_dataArrTwo[(indexPath.row - 1)]source];
        cell.label2.text = [_dataArrTwo[(indexPath.row - 1)]title];
        cell.label3.text = [_dataArrTwo[(indexPath.row - 1)]author];
        cell.label4.text = [_dataArrTwo[(indexPath.row - 1)]pubdate];
        return cell;
    }
    else if(indexPath.row == 2)
    {
        UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil];
        self.webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 200, 320, 1000)];
        NSString *str = [NSString stringWithFormat:@"%@", [_dataArrThree[(indexPath.row - 2)]content]];
        _webView.scrollView.scrollEnabled = NO;
        [_webView loadHTMLString:str baseURL:nil];
        [self.tableView addSubview:_webView];
        return cell;
    }
    else
        return nil;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
